home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
cenvid
/
batch.cmm
< prev
next >
Wrap
Text File
|
1995-10-11
|
21KB
|
796 lines
/*
* Batch.cmm
*
* Interpret a DOS batch file using CEnvi. This should work on all CEnvi
* versions.
*/
/* ---------------------------------------------------------------------- */
usage()
{
printf("Use the BATCH command to interpret a DOS batch file under any system\n");
printf("that CEnvi supports.\n");
printf("Syntax:\n BATCH [filename] [args]\nwhere:\n");
printf(" filename The .BAT file to be interpreted.\n");
printf(" args Any number of arguments for the batch file.\n");
exit(EXIT_FAILURE);;
}
/* ---------------------------------------------------------------------- */
/*
* Setup sets up all system dependent variables. Later, when #ifdef is working
* this will be replaced using that.
*/
search_path = ""; // make it global
errorlevel = 0;
echo = 1; // echo on
setup()
{
if( defined(_NWNLM_) )
{
search_path = "SYS:/CENVI/";
} else {
if( defined(CMMPATH) ) search_path = CMMPATH;
}
}
/*
* Change the current directory to the given one. Return 0 if successful.
*/
my_chdir(newdir)
{
if( defined(_NWNLM_) ) return chdir(newdir);
if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
{
lReg.ah = 0x3B;
if !defined(_DOS32_)
lReg.ds = segment(newdir), lReg.dx = offset(newdir);
else
lReg.dx = pointer(newdir);
interrupt(0x21,lReg,out);
return out.ax;
}
if( defined(_NTCON_) || defined(_NTWIN_) )
{
return !DynamicLink("KERNEL32","SetCurrentDirectoryA",STDCALL,newdir);
}
if( defined(_OS2_) )
{
#define ORD_DOS32SETCURRENTDIR 255
return DynamicLink("doscalls",ORD_DOS32SETCURRENTDIR,BIT32,CDECL,newdir);
}
}
special_command = "__@%*&^%";
/* ---------------------------------------------------------------------- */
/*
* The count is the number of params, the array is the params. These
* correspond to %X where X is a digit. We also do %variable% for environment
* variables, all uppercase.
*
* To ease processing, we kill the '\n' too.
*/
substitute_line(line,count,array)
{
it = ""; i = 0; j = 0;
while( line[i] )
{
if( line[i]!='%' )
{
if( line[i]!='\n' )
{
it[j++] = line[i];
}
i++;
continue;
}
// Here we have a substitution...
if( isdigit(line[i+1]) )
{
i += 2;
num = line[i-1]-'0';
if( num>=count ) continue; // no such argument
for( k=0;array[num][k];k++ ) it[j++] = array[num][k];
} else {
if( line[++i]=='%' )
{
i++; it[j++] = '%'; continue;
}
env = ""; offset = 0;
for( ;line[i] && !isspace(line[i]) && line[i]!='%';i++ )
env[offset++] = line[i];
if( isspace(line[i]) )
{
printf("Variable is not terminated.\n");
return "";
}
env[offset] = 0; i++;
value = "";
sprintf(to_exec,"if( DataType(%s)==CMM_INT ) sprintf(value,\"%%d\",%s); else if( DataType(%s)==CMM_BYTE ) strcpy(value,%s);\n",env,env,env,env);
Interpret(to_exec,INTERP_TEXT);
while( value[0]!='\0' ) { it[j++] = value[0]; value++; }
}
}
it[j] = '\0';
return it;
}
/*
* Search the appropriate places for the given name. If found, return the
* full filename else return NULL.
*/
find_cmm(name)
{
// First search the exact filename
if( fp = fopen(name,"r") )
{
fclose(fp);
return FullPath(name);
}
// Then search the filename converted to '.cmm'
path = SplitFileName(name);
sprintf(newname,"%s%s.cmm",path.dir,path.name);
if( fp = fopen(newname,"r") )
{
fclose(fp);
return FullPath(newname);
}
// Then search the converted filename using the search path.
strcpy(tmppath,search_path);
while( tmppath[0]!='\0' )
{
while( tmppath[0]==';' ) tmppath++;
start = tmppath;
while( tmppath[0]!='\0' && tmppath[0]!=';' ) tmppath++;
tmppath[0] = '\0'; tmppath++;
sprintf(newname,"%s%c%s.cmm",start,strchr(start,'/')?'/':'\\',path.name);
if( fp = fopen(newname,"r") )
{
fclose(fp);
return FullPath(newname);
}
}
// Else not found
return NULL;
}
/* ---------------------------------------------------------------------- */
next_token(line,index)
{
command = ""; j = 0;
while( isspace(line[index]) ) index++;
while( line[index] && !isspace(line[index]) )
command[j++] = line[index++];
while( isspace(line[index]) ) index++;
command[j] = '\0';
return command;
}
/* ---------------------------------------------------------------------- */
// All these functions return the name of the label we have to GOTO, or ""
// if none.
/*
* We just ignore the line.
*/
REM_handler(the_line,ptr,line_num)
{
return "";
}
/*
* We dump the text to the terminal. The exceptions are if we have 'echo on'
* or 'echo off'.
*/
ECHO_handler(the_line,ptr,line_num)
{
if( !stricmp(the_line+ptr,"ON") )
{
echo = 1;
return "";
}
if( !stricmp(the_line+ptr,"OFF") )
{
echo = 0;
return "";
}
printf("%s\n",the_line+ptr);
return "";
}
/*
* If no parameter, print the current directory. Otherwise, set the current
* directory to what is given.
*/
CD_handler(the_line,ptr,line_num)
{
if( the_line[ptr]=='\0' )
{
printf("%s\n",getcwd(buffer));
return "";
}
if( my_chdir(the_line+ptr) )
printf("The system cannot find the path specified at line %d.\n",line_num);
return "";
}
/*
* Simply clear the screen.
*/
CLS_handler(the_line,ptr,line_num)
{
ScreenClear();
return "";
}
/*
* TYPE a file to the terminal. This is an incredibly simple routine. Use
* MORE for some options.
*/
TYPE_handler(the_line,ptr,line_num)
{
if( fp = fopen(the_line+ptr,"r") )
{
while( 1 )
{
if( (newline = fgets(fp))==NULL ) break;
printf("%s",newline);
}
fclose(fp);
} else {
printf("Unable to open file \"%s\" at line %d.\n",the_line+ptr,line_num);
}
return "";
}
/*
* Since the variable we need to change is a local to one of our callers,
* we pass back a command to it to tell it to do what we want.
*/
SHIFT_handler(the_line,ptr,line_num)
{
return special_command;
}
/*
* Just waits for the user to press any key.
*/
PAUSE_handler(the_line,ptr,line_num)
{
printf("Press any key when ready . . . "); fflush(stdout);
getch();
printf("\n");
return "";
}
/*
* Invoke a new batch file and return to this one when it finishes
*/
CALL_handler(the_line,ptr,line_num)
{
// Set up a new argc/argv array and call main again.
strcpy(newargv[0],"CEnvi");
newargc = 1;
while( the_line[ptr]!='\0' )
{
command = next_token(the_line,ptr);
strcpy(newargv[newargc++],command);
while( isspace(the_line[ptr]) ) ptr++;
}
main(newargc,newargv);
return "";
}
/*
* The _EVIL_ goto command...
*/
GOTO_handler(the_line,ptr,line_num)
{
return the_line+ptr;
}
/*
* No, this does not simulate a person skilled in dealing with evil
* Egyptian gods...
*/
SET_handler(the_line,ptr,line_num)
{
if( defined(_NWNLM_) )
{
printf("Netware has no environment variables at line %d.\n",line_num);
return "";
}
if( the_line[ptr]=='\0' )
{
array = getenv();
for( ii=0;ii<GetArraySpan(array);ii++ )
{
printf("%s=%s\n",array[ii],getenv(array[ii]));
}
return "";
}
// Else we extract the name and see if we have a '=' or not.
command = next_token(the_line,ptr);
if( (where = strchr(command,'='))!=NULL )
{
where[0] = '\0';
strcpy(env_var,command);
strcpy(new_val